import 'dart:math';

import 'package:afterclass01/components/home_banner.dart';
import 'package:afterclass01/components/home_nav.dart';
import 'package:afterclass01/components/home_search_bar.dart';
import 'package:afterclass01/components/home_shop_list.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class homeIndex extends StatefulWidget {
  const homeIndex({Key? key}) : super(key: key);

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<homeIndex> {
  Map resData = {};
  //  定义列表存储轮播图数据
  List bannerList = [];
  // 定义列表存储导航栏数据
  List navList = [];
  // 定义列表存储广告数据
  List adList = [];
  // 定义列表存储商品数据
  List goodsList = [];

  @override
  //调用生命周期
  void initState() {
    getResData();
    super.initState();
  }

  void getResData() async {
    Dio dio = Dio();
    Response res = await dio
        .get('http://smart-shop.itheima.net/index.php?s=/api/page/detail');
    setState(() {
      resData = res.data['data']['pageData'];
      bannerList = resData['items'][1]['data'];
      navList = resData['items'][3]['data'];
      //  广告图片数据
      adList = resData['items'][4]['data'];
      // 商品数据
      goodsList = resData['items'][6]['data'];
      print(resData['items'][6]['data']);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //标题居中
        centerTitle: true,
        backgroundColor: Colors.red,
        title: Text(
          '智慧商城',
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: ListView(
        children: [
          HomeSearchBar(),
          HomeBanner(bannerList: bannerList),
          HomeNav(navList: navList),
          //判断是否有广告
          adList.isNotEmpty
              ? Padding(
                  padding: EdgeInsets.only(top: 5, bottom: 5),
                  child: Image.network('${adList[0]['imgUrl']}'),
                )
              : Container(),
          //猜你喜欢
          Padding(
              padding: EdgeInsets.only(top: 5, bottom: 5),
              child: Container(
                  alignment: Alignment.center, child: Text('—— 猜你喜欢 ——'))),
          HomeShopList(goodsList: goodsList)
        ],
      ),
    );
  }
}
